home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / thenet / x1j4_src / thenetpc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-06  |  12.3 KB  |  580 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include "..\all.h"
  4. #include "..\tntyp.h"        /* Definition of structures             */
  5. #include "..\ip.h"    
  6. #include "..\icmp.h"
  7.  
  8. ARP arp;
  9.  
  10.  
  11. /* *********************************************************************
  12.  * simple test harness for thenet x-1j
  13.  * (c) D Roberts G8KBB 1991 
  14.  * you are free to use this for amateur, non commercial non profit use only
  15.  *
  16.  * It allows the thenet software to be run on a PC connected to a KISS
  17.  * tnc. It does not support all features and is designed to allow test
  18.  * procedures on the protocols. It should ideally be run under a debugger.
  19.  * To exit the program, use control-b. DO NOT USE CONTROL-C !!!!!!!!!!!!
  20.  *
  21.  * ********************************************************************/
  22.  
  23. /* *********************************************************************
  24.  * set default parameters in place of tnl1.mac routines
  25.  *
  26.  * ********************************************************************/
  27.  
  28. unsigned DEFTLI = 10;
  29. unsigned DEFCH0 = 10;
  30. unsigned DEFCH1 = 10;
  31. unsigned DEFCON = 4;
  32. unsigned DEFDES = 100;
  33. unsigned DEFPER = 64;
  34. unsigned DEFTBS = 180;
  35. unsigned DEFTWI = 4;
  36. unsigned DEFSLO = 10;
  37. unsigned DEFTTO = 300;
  38. unsigned DEFLT2 = 100;
  39. unsigned DEFLT3 = 18000;
  40. unsigned DEFL2T = 10;
  41. unsigned DEFTTR = 2;
  42. unsigned DEFWQU = 1;
  43. unsigned DEFBEA = 2;
  44. unsigned DEFOBB = 5;
  45. unsigned DEFOBC = 6;
  46. unsigned DEFTAC = 6;
  47. unsigned DEFCQ  = 1;
  48. unsigned DEFMAF = 2;
  49. unsigned DEFNOA = 900;
  50. unsigned DEFRPA = 1;        /* enable digi for test reasons */
  51. unsigned DEFRAK = 5;
  52. unsigned DEFVAL = 1;
  53. unsigned DEFBRI = 1800;
  54. unsigned char DEFXFP = 0;
  55. unsigned char DEFDPA = 0;
  56. unsigned char DEFTP = 30;
  57. unsigned char DEFESC = 0x1b;
  58. unsigned char DEFIDE[6] = {'#','E','S','D','G','#'};
  59. unsigned char DEFCAL[] = {'G','8','K','B','B',' ', 0x60 };
  60. unsigned char DEFINF[]  = {"ESDG\rTheNet X-1G ( based on TheNet 1.01 )\rTest version by G8KBB"};
  61. unsigned char DEFPWD[]  = {"QPCGTIAORENBYSWI49DH10935D 5639DJCFHG   JFHGUH863TGDGBSHGQHQJSLFDL 58FJDHDTSR   "};
  62.  
  63. unsigned char DEICNT = 0;
  64. unsigned      DEFCW = 1800;
  65. unsigned char DEFKIS = 0;
  66. unsigned char DEFHOS = 0;
  67. unsigned char DEFBBS = 1;
  68. unsigned char DEFCWS = 7;
  69. unsigned char DEFMHL = 10;
  70. unsigned char DEFBCN = 3;
  71. unsigned char DEFALG = 0;
  72. unsigned DEFBR1 = 0;
  73. unsigned DEFBIN = 900;
  74. unsigned char DEFCSB = 0;
  75. unsigned DEFNOHASH = 0;
  76. unsigned char DEFHLP = 31;
  77. #ifdef IPROUTE
  78. unsigned long DEFIPBCAST = (44L<<24)+(131L<<16)+0;
  79. unsigned long DEFMYIPADDR = (44L<<24)+(131L<<16)+(16<<8)+83;
  80. unsigned char DEFIPTTL = 10;
  81. unsigned char DEFIPL2MODES = 3;
  82. unsigned char DEFIPENABLE = 1;
  83. #endif
  84. #ifdef CWID
  85. unsigned char calper, caltog;
  86. #endif
  87. #ifdef MOD_MTU
  88. unsigned DEF_MTU_IP0 = 256;
  89. unsigned DEF_MTU_IP1 = 256;
  90. unsigned DEF_MTU_IPN = 256-20;
  91. unsigned DEF_MTU_I_MAX = 257;
  92. unsigned DEF_MTU_L2_MAX = 256+72;
  93. #endif
  94. #ifdef METERS
  95. unsigned char DEFRECONNECT = 0;
  96. unsigned char DEFNO_SLIME = 0;
  97. unsigned char DEFNO_DIGI = 0;
  98. unsigned char DEFDEVMETER = 255;
  99. unsigned      DEFMETERFLAGS = 3;
  100. unsigned char DEFRXSIGMIN = 50;
  101. unsigned char DEFRXSMETER = 100;
  102. unsigned char DEFRXDB = 50;
  103. signed char DEFDBFLOOR = 120;
  104. unsigned char DEFMULT1 = 1;
  105. unsigned char DEFMULT2 = 100;
  106. unsigned char DEFOFS1 = 0;
  107. unsigned char DEFOFS2 = 127;
  108. unsigned char sig_level;
  109. #endif
  110.  
  111. char beadil[] = "";
  112.  
  113. #ifdef MANAGED
  114. char AudCon[] = "C ";
  115. char AudConAcc[] = "CA";
  116. char AudDisc[] = "D ";
  117. char AudDiscAcc[] = "DA";
  118. #endif
  119.  
  120. char helpmsg[] = "This is the help text\nwhich is a bit thin in the demo!";
  121.  
  122.  
  123. /* *********************************************************************
  124.  * The 'memory' block simulaes ram in the TNC used for buffers
  125.  *
  126.  * ********************************************************************/
  127.  
  128. unsigned char memblock[32768];
  129.  
  130. /* *********************************************************************
  131.  * These are used in TheRing software modifications only
  132.  *
  133.  * ********************************************************************/
  134.  
  135. unsigned tnc_connected = 0;    /* 0 TO DISABLE RS232 XMIT, 1 TO ENABLE */
  136. int comport = 0;        /* 0 == COM1:, 1 == COM2: ETC */
  137. int cominit = 0xc3;        /* com port baud rate etc setup */
  138.  
  139. unsigned bauds[] = { 300,600,1200,2400,4800,9600 };
  140. unsigned char inits[] = { 0x43,0x63,0x83,0xa3,0xc3,0xe3 };
  141.  
  142. /* *********************************************************************
  143.  * start here in place of tnc power on reset.
  144.  * start the clock going, initialise rs232 link, clear memory block
  145.  * just in case, then execute mainf ( in place of tnl1.mac )
  146.  * ********************************************************************/
  147.  
  148. main( argc, argv )
  149. char *argv[];
  150. {
  151.     unsigned baud, channel, i;
  152.  
  153.     if( argc == 1 )
  154.         tnc_connected = 0;    /* 0 TO DISABLE RS232 XMIT, 1 TO ENABLE */
  155.     else
  156.     {
  157.         baud = atoi( argv[1] );
  158.         if( argc > 2 )
  159.         {
  160.             channel = atoi( argv[2] );
  161.             if( channel < 1 || channel > 4 )
  162.             {
  163.                 printf("Error - invalid channel number\n");
  164.                 exit(1);
  165.             }
  166.             comport = channel-1;
  167.         }
  168.         for(i=0;i<sizeof(bauds);i++)
  169.             if( baud == bauds[i] )
  170.                 break;
  171.         if( i == sizeof( bauds ) )
  172.         {
  173.             printf("Error - invalid baud rate\n");
  174.             exit(2);
  175.         }
  176.         cominit = inits[i];
  177.         tnc_connected = 1;
  178.     }
  179.     setticker();                    /* INSTALL TICK */
  180.     if( tnc_connected )
  181.         comsetup();                    /* INIT RS232 */
  182.     memset( memblock, 0, sizeof( memblock ) );    /* ENSURE COLD START */
  183.     mainf();                    /* START TheNet */
  184. }
  185.  
  186. /* ******************************************************************* *
  187.  * ERRORS OR MISSING BITS FROM THE SOURCE CODE AND OTHER CORRECTIONS   *
  188.  * ******************************************************************* */
  189.  
  190. seades( call )
  191. char *call;
  192. {
  193.     return( iscall( call ) );
  194. }
  195.  
  196. /* *********************************************************************
  197.  * SIMPLE VERSIONS OF THENET RUNTIME ROUTINES FOR TEST HARNESS       
  198.  *
  199.  *
  200.  * ********************************************************************/
  201.  
  202. reset()
  203. {
  204.     L1INIT();
  205. }
  206.  
  207. L1INIT()
  208. {
  209.     HOINIT();
  210. }
  211.  
  212. HOINIT()
  213. {
  214. }
  215.  
  216. /* return true if there is a keyboard char ready from the pc
  217.  * as if it has come from the host port
  218.  */
  219.  
  220. ishget()
  221. {
  222.     return( kbhit() & 1 );
  223. }
  224.  
  225. ishput()
  226. {
  227.     return( 0 );
  228. }
  229.  
  230. /* read a keyboard character. If it is control-b, clean up and exit.
  231.  * If not, return the character
  232.  */
  233.  
  234. hgetc()
  235. {
  236.     int c;
  237.     
  238.     c = getch();
  239.     
  240.     if( c == 2 )
  241.     {
  242.         removeticker();
  243.         exit();
  244.     }
  245.     return( c );
  246. }
  247.  
  248. /* display a character on the screen in lieu of the host port
  249. */
  250.  
  251. hputc(c)
  252. int c;
  253. {
  254.     putchar( c );
  255. }
  256.  
  257. /* simulate checking of the host port. If flag is for hldc port,
  258.  * say it is ready ( coz a KISS port will be !! ), but if it
  259.  * is for crossconnect, say it is not ( coz there is none here ! )
  260.  */
  261.  
  262. iscd( port )
  263. unsigned port;
  264. {
  265.     if( port == 1 )
  266.         return( 0 );
  267.     return( 1 );
  268. }
  269.  
  270. /* no need on the PC to enable interrupts, but go thru the motions 
  271.  */
  272.  
  273. decEI()
  274. {
  275.     DEICNT--;
  276. }
  277.  
  278. /* ditto no need to disable interrupts !
  279.  */
  280.  
  281. DIinc()
  282. {
  283.     DEICNT++;
  284. }
  285.  
  286. /* successful execution of the STAled and CONled routines
  287.  * gives a message on the screen
  288.  */
  289.  
  290. CONled( flag )
  291. unsigned flag;
  292. {
  293.     cprintf("\r\nCONNECT LED : %s\r\n", flag ? "ON" : "OFF" );
  294. }
  295.  
  296. STAled( flag )
  297. unsigned flag;
  298. {
  299.     cprintf("\r\nSTATUS LED : %s\r\n", flag ? "ON" : "OFF" );
  300. }
  301.  
  302. /* return a random 8 bit number
  303.  */
  304.  
  305. random()
  306. {
  307.     return( rand() & 0xff );
  308. }
  309.  
  310. /* simulate the limits of the free ram by pointing to the ram memory block
  311.  */
  312.  
  313. char *maxmem()
  314. {
  315.     return( &memblock[ sizeof( memblock ) - 1 ] );
  316. }
  317.  
  318. char *minmem()
  319. {
  320.     return( memblock );
  321. }
  322.  
  323. /* this is the main send routine, for a KISS tnc.
  324.  */
  325.  
  326. kicktx( port )
  327. {
  328.     unsigned int c;
  329.     
  330.     if( port )                /* host port */
  331.     {
  332.         outprintf("\r\nHost:", 0 );
  333.         do {                /* until eom mark, */
  334.             c = l1get(0x8101);    /* ask for data */
  335.             outprintf(" %2x",c);    /* and display it */
  336.         }while( c <= 0xff );
  337.         outprintf("\r\n", 0 );
  338.     }
  339.     else                    /* hdlc port */
  340.     {
  341.         outprintf("\r\nHDLC:", 0);
  342.         hdlcout( 0xc0 );        /* send KISS start */
  343.         hdlcout( 0 );            /* send KISS data signal */
  344.         do {                /* until eom mark, */
  345.             c = l1get(0);        /* get a byte of data */
  346.             if( c <= 0xff )        /* if valid data, then */
  347.                 hdlcsend( c );    /* send to TNC */
  348.         }while( c <= 0xff );        /* repeat for whle frame */
  349.         hdlcout( 0xc0 );        /* send KISS end signal */
  350.         outprintf("\r\n", 0 );
  351.     }
  352. }
  353.  
  354. /* as there is no difference between half and full duplex, there
  355.  * is no need here for pushtx(), but it should really be implemented.
  356.  */
  357.  
  358. pushtx()
  359. {
  360. }
  361.  
  362. /* **************************************************************** */
  363. /* KISS I/O OPERATIONS - OUTPUT AND INPUT                 */
  364.  
  365. int ipstate = 0;        /* state of the input routine */
  366.  
  367. inputdata()
  368. {
  369.     unsigned c;
  370.  
  371.     while( instatus() & 0x100 )    /* whilst there is input data */
  372.     {
  373.         c = indata();        /* get it from TNC */
  374.         switch( ipstate )    /* handle according to state */
  375.         {
  376.             case 0:        /* not receiving */
  377.                 if( c == 0xc0 )
  378.                 {
  379.                     inprintf("\r\nINPUT: %2x",c);
  380.                     ipstate = 3;
  381.                     l1put( 0x8001 );
  382.                 }
  383.                 break;
  384.             case 3:        /* wait for null */
  385.                 inprintf(" %2x",c);
  386.                 switch( c )
  387.                 {
  388.                     case 0:
  389.                         ipstate = 1;
  390.                         break;
  391.                     case 0xc0:
  392.                         l1put( 0x8001 );
  393.                         break;
  394.                     default:
  395.                         ipstate = 0;
  396.                 }
  397.                 break;
  398.             case 1:        /* inputting data */
  399.                 inprintf(" %2x",c);
  400.                 switch( c )
  401.                 {
  402.                     case 0xc0:
  403. #ifdef METERS
  404.                         sig_level = random();
  405. #endif
  406.                         l1put( 0x8000 );
  407.                         ipstate = 0;
  408.                         break;
  409.                     case 0xdb:
  410.                         ipstate = 2;
  411.                         break;
  412.                     default:
  413.                         l1put( c );
  414.                 }
  415.                 break;
  416.                 
  417.             case 2:        /* FESC KISS char */
  418.                 inprintf(" %2x",c);
  419.                 switch( c )
  420.                 {
  421.                     case 0xdc:
  422.                         l1put( 0xc0 );
  423.                         break;
  424.                     case 0xdd:
  425.                         l1put( 0xdb );
  426.                         break;
  427.                     default:
  428.                         l1put( c );
  429.                 }
  430.                 ipstate = 1;
  431.                 break;
  432.             default:
  433.                 inprintf(" %2x",c);
  434.                 l1put( c );
  435.         }
  436.     }
  437. }
  438.  
  439. /* send a character to hdlc port, with KISS ESCAPE processing
  440.  */
  441.  
  442. hdlcsend( c )
  443. {
  444.     switch( c )
  445.     {
  446.         case 0xc0:            /* if you get frame start */
  447.             hdlcout( 0xdb );    /* then send the esc char */
  448.             hdlcout( 0xdc );    /* followed by the char */
  449.             break;
  450.         case 0xdb:
  451.             hdlcout( 0xdb );    /* ditto for escape */
  452.             hdlcout( 0xdd );
  453.             break;
  454.         default:
  455.             hdlcout( c );
  456.     }
  457. }
  458.  
  459. hdlcout( c )
  460. {
  461.     outprintf(" %2x",c);
  462.     outdata( c );
  463. }
  464.  
  465.  
  466. /* **************************************************************** */
  467. /* LOW LEVEL RS232 I/O OPERATIONS - STATUS, OUTPUT, INPUT AND RESET */
  468.  
  469.  
  470.  
  471. instatus()
  472. {                                        
  473.     union REGS inregs, outregs;
  474.  
  475.     inregs.x.dx = comport;
  476.     inregs.h.ah = 3;
  477.     int86( 0x14, &inregs, &outregs );
  478.     return( outregs.x.ax );
  479. }
  480.  
  481. indata()
  482. {                                        
  483.     union REGS inregs, outregs;
  484.  
  485.     inregs.x.dx = comport;
  486.     inregs.h.ah = 2;
  487.     int86( 0x14, &inregs, &outregs );
  488.     return( outregs.h.al );
  489. }
  490.  
  491. outdata( c )
  492. {
  493.     union REGS inregs, outregs;
  494.     
  495.     inregs.h.ah = 1;
  496.     inregs.x.dx = comport;
  497.     inregs.h.al = c;
  498.     if( tnc_connected )
  499.         int86( 0x14, &inregs, &outregs );
  500. }
  501.  
  502. comsetup()
  503. {
  504.     union REGS inregs, outregs;
  505.     
  506.     inregs.h.ah = 0;
  507.     inregs.x.dx = comport;
  508.     inregs.h.al = cominit;
  509.     int86( 0x14, &inregs, &outregs );    /* reset the port */
  510.     inregs.h.ah = 4;
  511.     inregs.x.dx = comport;
  512.     inregs.h.al = 0;
  513.     int86( 0x14, &inregs, &outregs );    /* check combios loaded */
  514.     if( outregs.x.ax != 0xaa55 )
  515.     {
  516.         printf("Error - Please load combios first\n");
  517.         exit(1);
  518.     }
  519.     while( instatus() & 0x100 )        /* clear i/p buffer */
  520.         indata();
  521.     inregs.h.ah = 6;
  522.     inregs.x.dx = comport;
  523.     inregs.h.al = 0;
  524.     int86( 0x14, &inregs, &outregs );    /* raise rts & dtr */
  525. }
  526.  
  527.  
  528. /*********************************************************
  529.  * simple console debugging functions 
  530.  * ******************************************************/
  531.  
  532. int showinput = 0;    /* set to 0 to disable display if i/p frames */
  533. int showoutput = 0;    /* set to 0 do disable display of o/p frames */
  534.  
  535. inprintf( s, c )
  536. char *s;
  537. int c;
  538. {
  539.     if( showinput )
  540.         cprintf( s, c );
  541. }
  542.  
  543. outprintf( s, c )
  544. char *s;
  545. int c;
  546. {
  547.     if( showoutput )
  548.         cprintf( s, c );
  549. }
  550.  
  551. /* *************************************************************** */
  552.  
  553. #ifdef HOSTMODE
  554.  
  555. raisedcd()
  556. {
  557. }
  558.  
  559. dropdcd()
  560. {
  561. }
  562.  
  563. unsigned char SIBRR0;
  564.  
  565. ishost()
  566. {
  567.     return( 1 );
  568. }
  569.  
  570. #endif
  571.  
  572.  
  573. #ifdef CLOSEDOWN
  574. stop()
  575. {
  576.     removeticker();
  577.     exit(255);
  578. }
  579. #endif
  580.